package com.wisdom.benefit.jufa.main;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.base.BaseActivity;
import com.wisdom.benefit.jufa.base.BaseApplication;
import com.wisdom.benefit.jufa.net.RetrofitService;
import com.wisdom.benefit.jufa.utils.CommonUtils;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 余额明细
 */
public class DetailsBalanceActivity extends BaseActivity {
    private RelativeLayout startTl, endRl, backRl;
    public static final int DISTRICT = 101;
    public static final int TOWER = 102;
    private TextView startTv, endTv;
    private Button scerchBrn;
    private ListView lv;
    private DetailsBalanceAdapter detailsBalanceAdapter;
    private ArrayList<DetailsBalanceBean> datas;
    private LinearLayout ll;
    private String startTime, endTime;

    @Override
    protected int getLayout() {
        return R.layout.activity_details_balance;
    }

    @Override
    protected void initViews() {
        //开始时间
        startTl = findViewById(R.id.balance_start_time_rl);
        startTv = findViewById(R.id.balance_start_time_tv);
        //结束时间
        endRl = findViewById(R.id.balance_end_time_rl);
        endTv = findViewById(R.id.balance_end_time_tv);
        //搜索
        scerchBrn = findViewById(R.id.search_btn);
        //搜索结果
        ll = findViewById(R.id.details_balance_ll);
        //列表
        lv = findViewById(R.id.details_balance_lv);
        //返回按钮
        backRl = findViewById(R.id.details_balance_back);
        //设置监听
        setClick();
        //设置状态栏为蓝色
        YCAppBar.setStatusBarColor(DetailsBalanceActivity.this,
                ContextCompat.getColor(DetailsBalanceActivity.this,
                        R.color.blue));
    }

    private void setClick() {
        //开始时间
        startTl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent districtIntent1 = new Intent(DetailsBalanceActivity.this, CalendarViewActivity.class);
                districtIntent1.putExtra("flag", DISTRICT);
                startActivityForResult(districtIntent1, DISTRICT);
            }
        });
        //结束时间
        endRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent districtIntent2 = new Intent(DetailsBalanceActivity.this, CalendarViewActivity.class);
                districtIntent2.putExtra("flag", TOWER);
                startActivityForResult(districtIntent2, TOWER);
            }
        });
        //搜索
        scerchBrn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll.setVisibility(View.VISIBLE);
                if (startTv.getText().toString().equals("请选择开始时间")) {
                    startTime = "";
                } else {
                    startTime = startTv.getText().toString();
                }
                if (endTv.getText().toString().equals("请选择结束时间")) {
                    endTime = "";
                } else {
                    endTime = endTv.getText().toString();
                }
                userPassagewayList(Long.valueOf(BaseApplication.getContext().getUserId()).longValue(), startTime, endTime  );
            }
        });
        //返回按钮
        backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int getTitleStringRes() {
        return 0;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case DISTRICT:
                    String district = data.getStringExtra("district");//开始时间
                    startTv.setText(district);
                    if (!startTv.getText().toString().equals("请选择开始时间") && !endTv.getText().toString().equals("请选择结束时间")) {
                        scerchBrn.setEnabled(true);
                    } else {
                        scerchBrn.setEnabled(false);
                    }
                    break;
                case TOWER:
                    String tower = data.getStringExtra("tower");//结束时间
                    endTv.setText(tower);
                    if (!startTv.getText().toString().equals("请选择开始时间") && !endTv.getText().toString().equals("请选择结束时间")) {
                        scerchBrn.setEnabled(true);
                    } else {
                        scerchBrn.setEnabled(false);
                    }
                    break;
            }
        }
    }

    /**
     * 订单按条件查询接口
     */
    private void userPassagewayList(Long payeeId, String startTime, String endTime) {
        addDisposable(RetrofitService.getNetService()
                .userPassagewayList(Long.valueOf(BaseApplication.getContext().getUserId()).longValue(), startTime, endTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getCode() == 0) {
                        //列表
                        detailsBalanceAdapter = new DetailsBalanceAdapter(response.getData(), getActivity());

                        lv.setAdapter(detailsBalanceAdapter);
                    } else {
                        showBindToast(response.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    if (CommonUtils.isNetworkAvailable(getContext()))
                        showBindToast("查询失败");
                    else
                        showBindToast("无网络连接，请稍后再试");
                }));
    }
}
